class Admin::MembershipsController < AdminController
  def index
    @memberships = Membership.all.order("discount ASC").page(params[:page]).per(params[:per])
  end

  def new
    @membership = Membership.new
  end

  def create
    @membership = Membership.new membership_params
    if @membership.save
      redirect_to admin_memberships_path, notice: "保存成功！"
    else
      redirect_to admin_memberships_path, notice: "保存失败！"
    end
  end

  def edit
    @membership = Membership.find params[:id]
  end

  def update
    @membership = Membership.find params[:id]
    if @membership.update membership_params
      redirect_to admin_memberships_path, notice: "更新成功！"
    else
      redirect_to admin_memberships_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @membership = Membership.find params[:id]
    if @membership.destroy
      redirect_to admin_memberships_path, notice: "删除成功."
    else
      redirect_to admin_memberships_path, notice: "删除失败."
    end
  end

  private

  def membership_params
    params.require(:membership).permit(:name, :avatar, :big_avatar, :discount, :price, :published)
  end
end
